<%@ page language="java" contentType="text/html; charset=windows-1256"
	pageEncoding="windows-1256"%>
<%@ page import="java.util.*"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type"
	content="text/html; charset=windows-1256">
<title>Insert title here</title>
</head>
<body>
	<form method='post' action="QuestionnaireFinal">
		<%
			String titre = (String) request.getAttribute("titre");
			String nbrQL = (String) request.getAttribute("nbrQL");
			String nbrQM = (String) request.getAttribute("nbrQM");
			String nbrQU = (String) request.getAttribute("nbrQU");
			out.print("<center><font size='20'><B>" + titre
					+ "</font></center><BR>");
			out.print("<input type='hidden' name='titre' value='");
			out.print(titre + "'/>");
			out.print("<input type='hidden' name='nbrQL' value='");
			out.print(nbrQL + "'/>");
			out.print("<input type='hidden' name='nbrQM' value='");
			out.print(nbrQM + "'/>");
			out.print("<input type='hidden' name='nbrQU' value='");
			out.print(nbrQU + "'/>");
		
			
			
			
			TreeMap tm = (TreeMap) request.getAttribute("TM");
			List ql = (List) request.getAttribute("QL");
			HashMap qm = (HashMap) request.getAttribute("QM");
			HashMap qu = (HashMap) request.getAttribute("QU");

			Set keys = tm.keySet();
			int tab[] = new int[tm.size()];
			String[] tabQ=new String[tm.size()];
			//arranger les ordres
			int b = 0;
			
			//remplir le tableau des indices
			for (Iterator i = keys.iterator(); i.hasNext();) {

				String key = (String) i.next();

				int x = key.lastIndexOf('Q');
				String Q=key.charAt(x)+""+key.charAt(x+1);
				String res = "";
				for (int g = 0; g < x; g++) {
					res += key.charAt(g);
				}

				int resf = Integer.parseInt(res);
				tabQ[b]=Q;
				tab[b] = resf;
				b++;
			}
			//arranger le tableau des indices
			for (int j = 0; j < tab.length; j++) {
				int min = j;
				
				for (int i = j + 1; i < tab.length; i++) {
					if (tab[i] < tab[min]) {
						min=i;
					
					}
					
					}
				if (min!=j){
					int tampon=tab[j];
					tab[j]=tab[min];
					tab[min]=tampon;
					String tampon1=tabQ[j];
					tabQ[j]=tabQ[min];
					tabQ[min]=tampon1;
				}
				
				
			}
		//end arranger le tableau des indices
	//*******************************************************************
	String cle="";
	for(int g=0;g<tab.length;g++)
	{
		cle=tab[g]+tabQ[g];
		
		if(cle.endsWith("QL"))
		{
			
			// Les questions libres
			Iterator it = ql.iterator();
			int k = 0;

			while (it.hasNext()) {
				String QL = "ql";
				QL += k;
				k++;
				String libre = (String) it.next();
				
				if (tm.get(cle).equals(libre)) {
					
					out.print("<input type='text' size='40' value='");
					out.print(libre + "'name='");
					out.print(QL + "'/><br>");
					
					out.print("<input type='hidden' value='");
					out.print(tm.get(cle) + "'name='");
					out.print(cle + "'/>");

				}
			
			}//end while (it.hasNext())
	}//END if(cle.endsWith("QL"))
		
		
	if(cle.endsWith("QM"))
	{
		
		
							//Question multiples  

					Iterator it1 = qm.entrySet().iterator();
					int u = 0;

					while (it1.hasNext()) {
						String QM = "qm";
						String pos = "pos";
						QM += u;
						pos += u;

						Map.Entry pairs1 = (Map.Entry) it1.next();
						String multiple = (String) pairs1.getKey();

						if (tm.get(cle).equals(multiple)) {
							int nbrpos1 = Integer.parseInt((String) pairs1
									.getValue());
							out.print("<input type='hidden' value='");
							out.print(nbrpos1 + "'name='");
							out.print(pos + "'/>");
							out.print(multiple + "<br>");

							out.print("<input type='hidden' value='");
							out.print(multiple + "' name='");
							out.print(QM + "'/>");
							
							out.print("<input type='hidden' value='");
							out.print(tm.get(cle) + "'name='");
							out.print(cle + "'/>");
							
							for (int ii = 0; ii < nbrpos1; ii++) {
								String posM = "posM";
								posM += u;
								posM += ii;
								out.print("<input type='text' name='");
								out.print(posM + "'");
								out.print(" size='40'/>");
								out.print("<BR>");
							}
						}
		u++;
		}//end while (it1.hasNext())
}//END if(cle.endsWith("QM"))
	
	
	
	
	
	
if(cle.endsWith("QU"))
{
	
	Iterator it2 = qu.entrySet().iterator();
	int v = 0;

	while (it2.hasNext()) {
		String QU = "qu";
		String posbU = "posbU";
		QU += v;
		posbU += v;
		Map.Entry pairs2 = (Map.Entry) it2.next();
		String unique = (String) pairs2.getKey();

		if (tm.get(cle).equals(unique)) {
			int nbrpos2 = Integer.parseInt((String) pairs2
					.getValue());
			out.print("<input type='hidden' value='");
			out.print(nbrpos2 + "'name='");
			out.print(posbU + "'/>");
			out.print((String) pairs2.getKey() + "<br>");
			out.print("<input type='hidden' value='");
			out.print(unique + "' name='");
			out.print(QU + "'/>");
			
			out.print("<input type='hidden' value='");
			out.print(tm.get(cle) + "'name='");
			out.print(cle + "'/>");
			
			for (int j = 0; j < nbrpos2; j++) {
				String posU = "posU";
				posU += v;
				posU += j;
				out.print("<input type='text' name='");
				out.print(posU + "'");
				out.print(" size='40'/>");
				out.print("<BR>");
			}

		}//// end if(value.equals(unique) )

		v++;
	}//end While (it2.hasNext())
	
	
	
	
	
}//end if(cle.endsWith("QU"))
	
	
	
	
	
	
	
	
	}//end for(int g=0;g<tab.length;g++)
	//********************************************************************
		%>
		<input type="submit" Value="Voir Questionnaire" />
	</form>
</body>
</html>